package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.listener.OnItemClickListener;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.utils.MyHandlerUtil;
import com.hisan.freeride.common.view.MyLinearLayoutManager;
import com.hisan.freeride.databinding.MessageBinding;
import com.hisan.freeride.home.adapter.MessageAdapter;
import com.hisan.freeride.home.model.WoMessage;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.lzy.okgo.OkGo;

import java.util.ArrayList;
import java.util.List;


/**
 * 创建时间 : 2017/6/8
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：爱心消息页面
 */
public class WoMessageActivity extends BaseActivity<MessageBinding> {
    private int page=1;
    private MessageAdapter adapter;

    private List<WoMessage> wlist=new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.message;
    }

    @Override
    protected void initView() {
        mBinding.setStateModel(mStateModel);
        adapter=new MessageAdapter(WoMessageActivity.this,wlist);
        mBinding.rv.setLayoutManager(new MyLinearLayoutManager(WoMessageActivity.this));
        mBinding.rv.setAdapter(adapter);
        mBinding.rv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                TextView tid= (TextView) view.findViewById(R.id.message_id);
                Bundle bundle=new Bundle();
                bundle.putInt("id", Integer.parseInt(tid.getText().toString()));
                startActivityForResult(DLoveMessageActivtity.class,99,bundle,true);
            }
            @Override
            public void onDeleteClick(int position) {
                if (!CollectionUtils.isNullOrEmpty(wlist)){
                    Delete(wlist.get(position).getId());
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
        mBinding.pull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                page=1;
                wlist.clear();
                loadData(true);
                mBinding.pull.finishRefresh();

            }

            @Override
            public void loadMore() {
                page++;
                loadData(true);
                mBinding.pull.finishLoadMore();
            }
        });
    }

    @Override
    protected void loadData(boolean isRefresh) {
        OkGo.<List<WoMessage>>get(AppConfig.MyMessage)
                .tag(this)
                .params("page",page)
                .params("size",20)
                .execute(new DialogCallback<List<WoMessage>>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<List<WoMessage>> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            showData();
                            if(response.body().size()>=20){
                                mBinding.pull.setCanLoadMore(true);
                            }else {
                                mBinding.pull.setCanLoadMore(false);
                            }
                            if (isOk(response)){
                                wlist.addAll(response.body());
                                adapter.setData(wlist);
                            }else {
                                showErrorView(response.code());
                            }
                        }else{
                            if (!CollectionUtils.isNullOrEmpty(wlist)){
                                adapter=new MessageAdapter(WoMessageActivity.this,wlist);
                                mBinding.rv.setLayoutManager(new MyLinearLayoutManager(WoMessageActivity.this));
                                mBinding.rv.setAdapter(adapter);
                            }else noData();
                        }
                    }
                    @Override
                    public void onError(com.lzy.okgo.model.Response<List<WoMessage>> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }

    //删除消息
    private void Delete(int getid) {
        OkGo.delete(AppConfig.Delmessage)
                .tag(this)
                .params("id",getid)
                .execute(new DialogCallback<Object>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response)){
                            if (isOk(response)){
                                wlist.clear();

                                showSuccessToast("删除成功");
                                MyHandlerUtil.getHandler().postDelayed(() -> {
                                    page=1;
                                    loadData(true);
                                },200);
                            }else {
                                showError(response);
                            }
                        }
                    }
                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });
    }

}
